package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domain.Contact;
import domain.DAOContact;

/**
 * Servlet implementation class UpdateServlet
 */
public class UpdateServlet extends ContactServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		String idStr = request.getParameter("id");
		String firstName = request.getParameter("firstName");
		String secondName = request.getParameter("secondName");
		String email = request.getParameter("email");
		
		int id;
		try{
			id = Integer.parseInt(idStr);
		}catch(NumberFormatException e){
			out.write(generateResponsePage("Update result", 
						"Error : " + idStr + " is not a number."));
			out.close();
			return;
		}
		
		DAOContact.updateContact(id, firstName, secondName, email);
		out.write(generateResponsePage("Update result", "Contact updated."));
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		String idStr = request.getParameter("id");
		
		int id;
		try{
			id = Integer.parseInt(idStr);
		}catch(NumberFormatException e){
			out.write(generateResponsePage("Update result", 
						"Error : " + idStr + " is not a number."));
			out.close();
			return;
		}
		
		Contact[] res = DAOContact.searchContact(id);
		Contact c;
		if(res.length == 0){
			out.write(generateResponsePage("Update result", "Not found."));
		}else{
			String data = "<table border=\"1\">" +
						"<tr><td>ID</td><td>First name</td>" +
						"<td>Second name</td><td>Email</td></tr>";
			c = res[0];
			if(c == null){
				out.write(generateResponsePage("Update result", data));
				out.close();
				return;
			}
			data += "<form action=\"\" method=\"Get\">";
			data += "<tr>" +
					"<td><input type=\"text\" value=\"" + c.getId() + "\" name=\"id\"></td>" +
					"<td><input type=\"text\" value=\"" + c.getFirstName() + "\" name=\"firstName\"></td>" +
					"<td><input type=\"text\" value=\"" + c.getSecondName() + "\" name=\"secondName\"></td>" +
					"<td><input type=\"text\" value=\"" + c.getEmail() + "\" name=\"email\"></td>" +
					"</tr>";	
			data += "</table>" +
					"<input type=\"submit\" + value=\"Update\"/>" +
					"</form>";
			out.write(generateResponsePage("Update result", data));
		}
		
		out.close();	
	}

}
